home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 6 code / TCP / NewsWatcher / NW Source / Shared Code / Reusable Source / arrowpair.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-20  |  10.6 KB  |  386 lines  |  [TEXT/MMCC]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     arrowpair.c
  4.  
  5.     This reusable module implements "arrow pair" controls. These controls
  6.     can be used for go backwards / go forwards operations. The arrows
  7.     can be an up/down pair or a left/right pair.
  8.     
  9.     This could also be done with a cdef, of course.
  10.     
  11.     Copyright © 1994-1995, Northwestern University.
  12.  
  13. ----------------------------------------------------------------------------*/
  14.  
  15. #include "arrowpair.h"
  16. #include "memutil.h"
  17. #include "drawutil.h"
  18.  
  19.  
  20.  
  21. typedef struct TPairInfo {
  22.     Boolean upDown;                    /* true if up/down, false if left/right */
  23.     Rect arrowRect;                    /* rectangle enclosing the arrows */
  24.     Rect backwardHotRect;            /* hot rect for backward arrow */
  25.     Rect forwardHotRect;            /* hot rect for forward arrow */
  26.     PolyHandle backwardArrow;        /* handle to backward arrow polygon */
  27.     PolyHandle forwardArrow;        /* handle to forward arrow polygon */
  28.     Boolean backwardEnabled;        /* true if backward arrow enabled */
  29.     Boolean forwardEnabled;            /* true if forward arrow enabled */
  30. } TPairInfo;
  31.  
  32.  
  33.  
  34. static RGBColor gLightBlue = {0x9999, 0x9999, 0xFFFF};    /* light blue for filling arrows */
  35.  
  36.  
  37.  
  38. /*----------------------------------------------------------------------------
  39.     CreateArrowPair
  40.     
  41.     Create an arrow pair.
  42.     
  43.     Entry:    upDown = true if up/down arrows, false if left/right.
  44.             arrowRect = pointer to rectangle enclosing the arrows.
  45.             backwardHotRect = pointer to hot rectangle for backward 
  46.                 arrow (up/left).
  47.             forwardHotRect = pointer to hot rectangle for forward 
  48.                 arrow (down/right).
  49.             
  50.     Exit:    function result = error code.
  51.             *arrowPair = new arrow pair reference.
  52. ----------------------------------------------------------------------------*/
  53.  
  54. OSErr CreateArrowPair (Boolean upDown, Rect *arrowRect, Rect *backwardHotRect,
  55.     Rect *forwardHotRect, ArrowPairRef *arrowPair)
  56. {
  57.     OSErr err = noErr;
  58.     TPairInfo **t;
  59.     PolyHandle p;
  60.     short height, indent;
  61.  
  62.     err = MyNewHandle(sizeof(TPairInfo), &t);
  63.     if (err != noErr) return err;
  64.     (**t).upDown = upDown;
  65.     (**t).arrowRect = *arrowRect;
  66.     (**t).backwardHotRect = *backwardHotRect;
  67.     (**t).forwardHotRect = *forwardHotRect;
  68.     (**t).backwardEnabled = true;
  69.     (**t).forwardEnabled = true;
  70.     
  71.     if (upDown) {
  72.         height = (arrowRect->right - arrowRect->left) >> 1;
  73.     } else {
  74.         height = (arrowRect->bottom - arrowRect->top) >> 1;
  75.     }
  76.     indent = height >> 1;
  77.     
  78.     p = OpenPoly();
  79.     if (upDown) {
  80.         MoveTo(arrowRect->left, arrowRect->top + height);
  81.         LineTo(arrowRect->left + indent, arrowRect->top + height);
  82.         LineTo(arrowRect->left + indent, arrowRect->top + height + indent);
  83.         LineTo(arrowRect->right - indent, arrowRect->top + height + indent);
  84.         LineTo(arrowRect->right - indent, arrowRect->top + height);
  85.         LineTo(arrowRect->right, arrowRect->top + height);
  86.         LineTo(arrowRect->left + height, arrowRect->top);
  87.         LineTo(arrowRect->left, arrowRect->top + height);
  88.     } else {
  89.         MoveTo(arrowRect->left + height, arrowRect->bottom);
  90.         LineTo(arrowRect->left + height, arrowRect->bottom - indent);
  91.         LineTo(arrowRect->left + height + indent, arrowRect->bottom - indent);
  92.         LineTo(arrowRect->left + height + indent, arrowRect->top + indent);
  93.         LineTo(arrowRect->left + height, arrowRect->top + indent);
  94.         LineTo(arrowRect->left + height, arrowRect->top);
  95.         LineTo(arrowRect->left, arrowRect->top + height);
  96.         LineTo(arrowRect->left + height, arrowRect->bottom);
  97.     }
  98.     ClosePoly();
  99.     (**t).backwardArrow = p;
  100.     
  101.     p = OpenPoly();
  102.     if (upDown) {
  103.         MoveTo(arrowRect->left, arrowRect->bottom - height);
  104.         LineTo(arrowRect->left + indent, arrowRect->bottom - height);
  105.         LineTo(arrowRect->left + indent, arrowRect->bottom - height - indent);
  106.         LineTo(arrowRect->right - indent, arrowRect->bottom - height - indent);
  107.         LineTo(arrowRect->right - indent, arrowRect->bottom - height);
  108.         LineTo(arrowRect->right, arrowRect->bottom - height);
  109.         LineTo(arrowRect->left + height, arrowRect->bottom);
  110.         LineTo(arrowRect->left, arrowRect->bottom - height);
  111.     } else {
  112.         MoveTo(arrowRect->right - height, arrowRect->bottom);
  113.         LineTo(arrowRect->right - height, arrowRect->bottom - indent);
  114.         LineTo(arrowRect->right - height - indent, arrowRect->bottom - indent);
  115.         LineTo(arrowRect->right - height - indent, arrowRect->top + indent);
  116.         LineTo(arrowRect->right - height, arrowRect->top + indent);
  117.         LineTo(arrowRect->right - height, arrowRect->top);
  118.         LineTo(arrowRect->right, arrowRect->top + height);
  119.         LineTo(arrowRect->right - height, arrowRect->bottom);
  120.     }
  121.     ClosePoly();
  122.     (**t).forwardArrow = p;
  123.     
  124.     *arrowPair = (ArrowPairRef)t;
  125.     return noErr;
  126. }
  127.  
  128.  
  129.  
  130. /*----------------------------------------------------------------------------
  131.     DisposeArrowPair
  132.     
  133.     Dispose an arrow pair.
  134.     
  135.     Entry:    arrowPair = arrow pair reference.
  136. ----------------------------------------------------------------------------*/
  137.  
  138. void DisposeArrowPair (ArrowPairRef arrowPair)
  139. {
  140.     TPairInfo **t;
  141.     
  142.     if (arrowPair == nil) return;
  143.     t = (TPairInfo**)arrowPair;
  144.     if ((**t).backwardArrow != nil) KillPoly((**t).backwardArrow);
  145.     if ((**t).forwardArrow != nil) KillPoly((**t).forwardArrow);
  146.     MyDisposeHandle(t);
  147. }
  148.  
  149.  
  150.  
  151. /*----------------------------------------------------------------------------
  152.     DrawArrowPair
  153.     
  154.     Draw an arrow pair.
  155.     
  156.     Entry:    arrowPair = arrow pair reference.
  157. ----------------------------------------------------------------------------*/
  158.  
  159. void DrawArrowPair (ArrowPairRef arrowPair)
  160. {
  161.     TPairInfo **t;
  162.     PolyHandle backwardArrow, forwardArrow;
  163.     
  164.     t = (TPairInfo**)arrowPair;
  165.     backwardArrow = (**t).backwardArrow;
  166.     forwardArrow = (**t).forwardArrow;
  167.     if ((**t).backwardEnabled) {
  168.         FillColorPoly(backwardArrow, &gLightBlue, &qd.gray);
  169.         FramePoly(backwardArrow);
  170.     } else {
  171.         FrameGrayPoly(backwardArrow);
  172.     }
  173.     if ((**t).forwardEnabled) {
  174.         FillColorPoly(forwardArrow, &gLightBlue, &qd.gray);
  175.         FramePoly(forwardArrow);
  176.     } else {
  177.         FrameGrayPoly(forwardArrow);
  178.     }
  179. }
  180.  
  181.  
  182.  
  183. /*----------------------------------------------------------------------------
  184.     TrackArrowPair
  185.     
  186.     Track an arrow pair.
  187.     
  188.     Entry:    arrowPair = arrow pair reference.
  189.     
  190.     Exit:    function result =
  191.                  0 if no hit.
  192.                 +1 if hit in forward arrow (down/right)
  193.                 -1 if hit in backward arrow (up/left)
  194. ----------------------------------------------------------------------------*/
  195.  
  196. short TrackArrowPair (ArrowPairRef arrowPair)
  197. {
  198.     TPairInfo **t;
  199.     Boolean filled;
  200.     Point where;
  201.     Rect hot;
  202.     PolyHandle p;
  203.     short result;
  204.     
  205.     t = (TPairInfo**)arrowPair;
  206.  
  207.     GetMouse(&where);
  208.     if (PtInRect(where, &(**t).backwardHotRect)) {
  209.         if (!(**t).backwardEnabled) return 0;
  210.         hot = (**t).backwardHotRect;
  211.         p = (**t).backwardArrow;
  212.         result = -1;
  213.     } else if (PtInRect(where, &(**t).forwardHotRect)) {
  214.         if (!(**t).forwardEnabled) return 0;
  215.         hot = (**t).forwardHotRect;
  216.         p = (**t).forwardArrow;
  217.         result = +1;
  218.     } else {
  219.         return 0;
  220.     }
  221.     FillPoly(p, &qd.black);
  222.     filled = true;
  223.     while (StillDown()) {
  224.         GetMouse(&where);
  225.         if (PtInRect(where, &hot)) {
  226.             if (!filled) {
  227.                 FillPoly(p, &qd.black);
  228.                 filled = true;
  229.             }
  230.         } else {
  231.             if (filled) {
  232.                 ErasePoly(p);
  233.                 FillColorPoly(p, &gLightBlue, &qd.gray);
  234.                 FramePoly(p);
  235.                 filled = false;
  236.             }
  237.         }
  238.     }
  239.     if (filled) {
  240.         ErasePoly(p);
  241.         FillColorPoly(p, &gLightBlue, &qd.gray);
  242.         FramePoly(p);
  243.         return result;
  244.     }
  245.     return 0;
  246. }
  247.  
  248.  
  249.  
  250. /*----------------------------------------------------------------------------
  251.     MoveArrowPair
  252.     
  253.     Move an arrow pair.
  254.     
  255.     Entry:    arrowPair = arrow pair reference.
  256.             botLeft = new coordinates of bottom/left corner of rectangle
  257.                 enclosing the arrows.
  258. ----------------------------------------------------------------------------*/
  259.  
  260. void MoveArrowPair (ArrowPairRef arrowPair, Point botLeft)
  261. {
  262.     TPairInfo **t;
  263.     short dh, dv;
  264.     
  265.     t = (TPairInfo**)arrowPair;
  266.     dh = botLeft.h - (**t).arrowRect.left;
  267.     dv = botLeft.v - (**t).arrowRect.bottom;
  268.     OffsetRect(&(**t).arrowRect, dh, dv);
  269.     OffsetRect(&(**t).backwardHotRect, dh, dv);
  270.     OffsetRect(&(**t).forwardHotRect, dh, dv);
  271.     OffsetPoly((**t).backwardArrow, dh, dv);
  272.     OffsetPoly((**t).forwardArrow, dh, dv);
  273. }
  274.  
  275.  
  276.  
  277. /*----------------------------------------------------------------------------
  278.     TestArrowPairHotRect
  279.     
  280.     Test to see if a location is inside an arrow pair hot rect.
  281.     
  282.     Entry:    arrowPair = arrow pair reference.
  283.             where = location in local coordinates.
  284.                 
  285.     Exit:    function result =
  286.                 -1 if location is in backward arrow hot rect.
  287.                  0 if location is not in a hot rect.
  288.                 +1 if location is in forward arrow hot rect.
  289.             *hotRect = hot rect if function result != 0.
  290.             *enabled = true if arrow is enabled if result != 0.
  291. ----------------------------------------------------------------------------*/
  292.  
  293. short TestArrowPairHotRect (ArrowPairRef arrowPair, Point where, Rect *hotRect,
  294.     Boolean *enabled)
  295. {
  296.     TPairInfo **t;
  297.     
  298.     if (arrowPair == nil) return 0;
  299.     t = (TPairInfo**)arrowPair;
  300.     if (PtInRect(where, &(**t).backwardHotRect)) {
  301.         *hotRect = (**t).backwardHotRect;
  302.         *enabled = (**t).backwardEnabled;
  303.         return - 1;
  304.     } else if (PtInRect(where, &(**t).forwardHotRect)) {
  305.         *hotRect = (**t).forwardHotRect;
  306.         *enabled = (**t).forwardEnabled;
  307.         return +1;
  308.     } else {
  309.         return 0;
  310.     }
  311. }
  312.  
  313.  
  314.  
  315. /*----------------------------------------------------------------------------
  316.     FlashArrowPair
  317.     
  318.     Flash an arrow pair.
  319.     
  320.     Entry:    arrowPair = arrow pair reference.
  321.             whichArrow =
  322.                 -1 to flash backward arrow (left/top).
  323.                 +1 to flash forward arrow (right/bottom).
  324.                 
  325.     Exit:    *hotRect = hot rect.
  326. ----------------------------------------------------------------------------*/
  327.  
  328. void FlashArrowPair (ArrowPairRef arrowPair, short whichArrow)
  329. {
  330.     TPairInfo **t;
  331.     PolyHandle p;
  332.     long myTicks;
  333.     
  334.     t = (TPairInfo**)arrowPair;
  335.     if (whichArrow == -1) {
  336.         if (!(**t).backwardEnabled) return;
  337.         p = (**t).backwardArrow;
  338.     } else {
  339.         if (!(**t).forwardEnabled) return;
  340.         p = (**t).forwardArrow;
  341.     }
  342.     FillPoly(p, &qd.black);
  343.     Delay(8, &myTicks);
  344.     ErasePoly(p);
  345.     FillColorPoly(p, &gLightBlue, &qd.gray);
  346.     FramePoly(p);
  347. }
  348.  
  349.  
  350.  
  351. /*----------------------------------------------------------------------------
  352.     EnableOrDisableArrowPair
  353.     
  354.     Enable or disable an arrow pair.
  355.     
  356.     Entry:    arrowPair = arrow pair reference.
  357.             whichArrow =
  358.                 -1 to enable/disable backward arrow (left/top).
  359.                 +1 to enable/disable forward arrow (right/bottom).
  360.             enable = true to enable arrow, false to disable it.
  361. ----------------------------------------------------------------------------*/
  362.  
  363. void EnableOrDisableArrowPair (ArrowPairRef arrowPair, short whichArrow, Boolean enable)
  364. {
  365.     TPairInfo **t;
  366.     PolyHandle p;
  367.     
  368.     t = (TPairInfo**)arrowPair;
  369.     if (whichArrow == -1) {
  370.         if ((**t).backwardEnabled == enable) return;
  371.         (**t).backwardEnabled = enable;
  372.         p = (**t).backwardArrow;
  373.     } else {
  374.         if ((**t).forwardEnabled == enable) return;
  375.         (**t).forwardEnabled = enable;
  376.         p = (**t).forwardArrow;
  377.     }
  378.     ErasePoly(p);
  379.     if (enable) {
  380.         FillColorPoly(p, &gLightBlue, &qd.gray);
  381.         FramePoly(p);
  382.     } else {
  383.         FrameGrayPoly(p);
  384.     }
  385. }
  386.